//
//  lxGoodsCell.m
//  luodansc
//
//  Created by 倫 星 on 2012/10/04.
//  Copyright (c) 2012年 方正株式会社. All rights reserved.
//

#import "lxGoodsCell.h"
#import "lxGoodsEntity.h"
#import "lxLabel.h"
#import "lxImageView.h"
#import <QuartzCore/QuartzCore.h>

@implementation lxGoodsCell

@synthesize image_goods_tubmb = _image_goods_tubmb;
@synthesize lb_goods_name = _lb_goods_name;
@synthesize lb_market_price = _lb_market_price;
@synthesize lb_shop_price = _lb_shop_price;
@synthesize image_comment_rank_img = _image_comment_rank_img;

- (id)initWithFrame:(CGRect)frame {
    self = [super initWithFrame:frame];
    if (self) {
        // Initialization code
        self.frame = frame;
        
        self.backgroundColor = [UIColor clearColor];
        
        float cell_width = self.bounds.size.width;
        float cell_height = self.bounds.size.height;
        
        int middle = 5;
        
        float img_width = (cell_width - middle * 2) / 4;
        float img_height = cell_height - middle * 2;
        
        float left_width = cell_width - middle * 3 - img_width;
        float row_height = img_height / 4;
        
        float lb_price_width = left_width / 5;
        if (ISIPADDEVICE) {
            lb_price_width = left_width / 10;
        }
        
        CGRect ret_image_goods_tubmb = CGRectMake(middle, middle, img_width, img_height);
        _image_goods_tubmb = [[lxImageView alloc] initWithFrame:ret_image_goods_tubmb];
        //圆角
        [_image_goods_tubmb.layer setMasksToBounds:YES];
        [_image_goods_tubmb.layer setCornerRadius:15];
        [_image_goods_tubmb setUserInteractionEnabled:NO];
        
        CGRect ret_lb_goods_name = CGRectMake(middle * 2 + img_width, middle  + row_height * 0, left_width, row_height);
        _lb_goods_name = [[lxLabel alloc] initWithFrame:ret_lb_goods_name];
        _lb_goods_name.adjustsFontSizeToFitWidth = YES;
        _lb_goods_name.textAlignment = NSTextAlignmentLeft;
        
        CGRect ret_lb_shop_price = CGRectMake(middle * 2 + img_width, middle + row_height * 2, lb_price_width, row_height);
        _lb_shop_price = [[lxLabel alloc] initWithFrame:ret_lb_shop_price];
        _lb_shop_price.adjustsFontSizeToFitWidth = YES;
        _lb_shop_price.textAlignment = NSTextAlignmentRight;
        
        CGRect ret_lb_market_price = CGRectMake((middle * 2 + img_width) + lb_price_width, middle + row_height * 2, lb_price_width, row_height);
        _lb_market_price = [[lxLabel alloc] initWithFrame:ret_lb_market_price];
        _lb_market_price.adjustsFontSizeToFitWidth = YES;
        _lb_market_price.textAlignment = NSTextAlignmentLeft;
        
        CGRect ret_image_comment_rank_img = CGRectMake(middle * 2 + img_width, middle + row_height * 3, left_width, row_height);
        _image_comment_rank_img = [[lxImageView alloc] initWithFrame:ret_image_comment_rank_img];
        [_image_comment_rank_img setLxAlignmentCenter:NO];
        [_image_comment_rank_img setUserInteractionEnabled:NO];
        
        [self addSubview:_image_goods_tubmb];
        [self addSubview:_lb_goods_name];
        [self addSubview:_lb_market_price];
        [self addSubview:_lb_shop_price];
        [self addSubview:_image_comment_rank_img];
    }
    return self;
}

- (BOOL)refreshData:(lxbaseEntity *)entity {
    BOOL result = [super refreshData:entity];
    if (NO == result) {
        return result;
    }
    
    lxGoodsEntity *lxentity = (lxGoodsEntity*)entity;
    NSString *goods_img = lxentity.goods_img;
    
    if (nil != goods_img) {
        NSString *str_url = [NSString stringWithFormat:@"%@%@", web_root, goods_img];

        [_image_goods_tubmb loadImageFromURL:str_url];
    }
    
    _lb_goods_name.text = lxentity.name;
    
    NSString *money = NSLocalizedString(@"money", nil);
        
    NSString *str_market_price = [lxStringUtility stringTrim:[NSString stringWithFormat:@"%10.2f", lxentity.market_price]];
    if (NO == [lxStringUtility isNil:str_market_price]) {
        _lb_market_price.textColor = [UIColor grayColor];
        _lb_market_price.isWithStrikeThrough = YES;
    } else {
        str_market_price = @"0";
    }
    _lb_market_price.text = [NSString stringWithFormat:@"%@%@", str_market_price, money];

    NSString *str_shop_price = [lxStringUtility stringTrim:[NSString stringWithFormat:@"%10.2f", lxentity.shop_price]];
    if (YES == [lxStringUtility isNil:str_shop_price]) {
        str_shop_price = @"0";
    }
    _lb_shop_price.text = [NSString stringWithFormat:@"%@%@", str_shop_price, money];
    _lb_shop_price.textColor = [UIColor redColor];
    
    NSString *comment_rank_img = lxentity.comment_rank_img;
    
    if (nil != comment_rank_img) {
        NSString *str_url = [NSString stringWithFormat:@"%@%@", web_root, comment_rank_img];
        
        [_image_comment_rank_img loadImageFromURL:str_url];
    }
    
    return YES;
}

- (void)dealloc {
    [_image_goods_tubmb release];
    [_lb_goods_name release];
    [_lb_market_price release];
    [_lb_shop_price release];
    [_image_comment_rank_img release];
    
    [super dealloc];
}


/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect
{
    // Drawing code
}
*/

@end
